package cn.maihe.elg.operation.service.bank;

import cn.hutool.core.bean.BeanUtil;
import cn.maihe.elg.operation.model.vo.bank.BankStatementDetailVo;
import cn.maihe.elg.operation.model.vo.bank.ListBankStatementVO;
import cn.maihe.elg.operation.repository.entity.*;
import cn.maihe.elg.operation.repository.mapper.SpdBankNoticeMapper;
import cn.maihe.elg.operation.service.BaseDaoService;
import cn.maihe.framework.starter.base.sql.PageBo;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @Description
 * @Author WJH
 * @Date 2025/5/27
 * */
@Service
@Slf4j
public class BankStatementService extends BaseDaoService<SpdBankNotice> {
    /**
     * 查询银行流水列表
     *
     * @param coreSerNo  交易流水号
     * @param acceptOrderNo  受理订单号
     * @param coreTransDate 交易日期
     * @param acctName 来账户名
     * @param acctNo 来帐账号
     * @return List<SpdBankNotice>
     * @author Alan
     */
    public PageBo<SpdBankNotice> selectBankStatementList(int pageNo, int pageSize, ListBankStatementVO vo){
        Map<String, Object> paramsMap = BeanUtil.beanToMap(vo);
        //出函时间
        if (vo.getCoreTransDateRange() != null && vo.getCoreTransDateRange().length == 2) {
            String[] rangeAy = vo.getCoreTransDateRange();
            if (StringUtils.isNoneBlank(rangeAy[0], rangeAy[1])) {
                paramsMap.put("startCoreTransDate", getTime(rangeAy[0]));
                paramsMap.put("endCoreTransDate", getTime(rangeAy[1]));
            }
        }
        PageBo<SpdBankNotice> pageBo = this.selectPageBySql("selectBankStatementList", "selectBankStatementCount", paramsMap, pageNo, pageSize);
        return pageBo;
    }


    /**
     * 根据id查询银行流水详情
     */
    public BankStatementDetailVo selectBankStatementById(Long id) {
        Assert.notNull(id, () -> "Id不能为空");
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", id);
        BankStatementDetailVo bankStatementDetailVo = this.selectOneBySql("selectBankStatementById", params);
        return bankStatementDetailVo;
    }

    /*
     * 将2024-01-15转为20240115
     * */
    public String getTime(String inputDate ){
        // 定义输入日期的格式
        DateTimeFormatter inputFormatter = DateTimeFormatter.ISO_LOCAL_DATE;

        // 解析输入的日期字符串为 LocalDate 对象
        LocalDate date = LocalDate.parse(inputDate, inputFormatter);

        // 定义输出日期的格式
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        // 格式化为目标字符串
        String outputDate = date.format(outputFormatter);

        return outputDate;
    }


}
